<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

/**
 * 礼物管理
 *
 * Created on 2021/12/30 18:06
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class GiftLibrary extends Base
{
    // classification礼物分类
    const CLASSIFICATION_OF_COMMON = 0;// 普通礼物
    const CLASSIFICATION_ONE = 1;// 普通礼物
    const CLASSIFICATION_TWO = 2;// 幸运礼物
    const CLASSIFICATION_THREE = 3;// 盲盒礼物
    const CLASSIFICATION_FOUR = 4;// 富豪值礼物
    const CLASSIFICATION_FIVE = 5;//彩蛋礼物
    const CLASSIFICATION_SIX = 6;//套盒礼物
    const CLASSIFICATION_SEVEN = 7;//新幸运礼物
    const CLASSIFICATION_EIGHT = 8;//增值服务
    const CLASSIFICATION_NINE = 9;//礼物抵扣券

    /**
     * 表名字
     *
     * @var string
     */
    protected $table = 'gift_library';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['gift_id', 'name', 'image', 'animate_resource', 'price', 'is_show', 'gift_sort', 'animate_resource_md5', 'gift_type', 'gift_rockon', 'is_red_pack_gift', 'ext_data', 'gift_label_id', 'is_special', 'classification', 'wealth_num'];

    /**
     * 构造函数
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 根据标签IDs查询礼物
     *
     * @param array $giftIds
     * @return array
     */
    public static function getCollectGiftsByIds(array $giftIds): array
    {
        return self::query()->whereIn('gift_id', $giftIds)->get()->keyBy('gift_id')->toArray();
    }

    /**
     * 获取礼物分类
     *
     * Created at 2021/12/30 18:30
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array[]
     */
    public function getGiftClassification(): array
    {
        return [
            ['value' => self::CLASSIFICATION_OF_COMMON, 'label' => '普通礼物'],
            ['value' => self::CLASSIFICATION_ONE, 'label' => '普通礼物'],
            ['value' => self::CLASSIFICATION_TWO, 'label' => '幸运礼物'],
            ['value' => self::CLASSIFICATION_THREE, 'label' => '盲盒礼物'],
            ['value' => self::CLASSIFICATION_FOUR, 'label' => '富豪值礼物'],
            ['value' => self::CLASSIFICATION_FIVE, 'label' => '彩蛋礼物'],
            ['value' => self::CLASSIFICATION_SIX, 'label' => '套盒礼物'],
            ['value' => self::CLASSIFICATION_SEVEN, 'label' => '新幸运礼物'],
            ['value' => self::CLASSIFICATION_EIGHT, 'label' => '增值服务'],
            ['value' => self::CLASSIFICATION_NINE, 'label' => '礼物抵扣券'],
        ];
    }

    // 获取CP等级礼物
    public function getGiftByClassification () {
        $rows = DB::table($this->table)
            ->where('classification', 3)
            ->where('is_show', 1)
            ->get();
        $gifts = self::objToArray($rows);
        return $gifts;
    }

    /**
     * 根据ID查询
     *
     * @param int $id
     * @return array
     */
    public static function getCollectById(int $id): array
    {
        $info = self::query()->where('gift_id', $id)->first();
        if(empty($info)) {
            return [];
        }
        return $info->toArray();
    }

}